package com.timesheet.gui.efforttracker.actions;

import com.timesheet.gui.efforttracker.Activator;
import com.timesheet.gui.efforttracker.controllers.SectionEffortCompositeController;
import com.timesheet.gui.efforttracker.utils.EffortTrackerUtil;
import com.timesheet.server.common.models.EffortTrackerDTO;
import com.timesheet.server.common.models.view.EffortTrackerViewModel;
import com.timesheet.serverice.EffortTrackerService;

import org.eclipse.jface.action.Action;
import org.eclipse.jface.viewers.StructuredSelection;

public class UpdateEffortAction extends Action {

	private SectionEffortCompositeController controller = null;

	EffortTrackerService service = new EffortTrackerService();

	public UpdateEffortAction(SectionEffortCompositeController controller) {
		setId("UpdateEffortAction");
		setText("Update Effort");
		this.controller = controller;
		setImageDescriptor(Activator.getImageDescriptor("icons/saveall_edit.gif"));

	}

	@Override
	public void run() {
		StructuredSelection selection =
				(StructuredSelection) this.controller.getNewEffortResultTableComposite().getTableViewer()
						.getSelection();
		if (selection.getFirstElement() instanceof EffortTrackerViewModel) {
			EffortTrackerViewModel vieModel = (EffortTrackerViewModel) selection.getFirstElement();
			EffortTrackerDTO effortTrackerDto = new EffortTrackerDTO();
			EffortTrackerDTO convertViewModel2Dto = EffortTrackerUtil.convertViewModel2Dto(vieModel, effortTrackerDto);
			service.update(convertViewModel2Dto);
		}

	}
}
